<template>
	<view>
		
		<!-- 归属店铺 -->
		<view>
			<view class="edit-info-box">
				<text class="info-name">店铺归属</text>
				<text class="uni-input info-content info-content-frist">{{old_beblong_info.code== '' ? '暂无店铺归属' : '('+ old_beblong_info.code +')' + old_beblong_info.site_name}}</text>
			</view>
			<view class="edit-info-box">
				<text class="info-name">店铺编号</text>
				<input class="uni-input info-content" placeholder="请输入店铺编号" v-model="new_belong_info.code" />
				<picker @change="bindPickerChange" :value="index" :range="shopList" range-key="site_name">
					<text class="desc uni-input"></text>
					<button type="primary" class="dynacode">附近商铺</button>
				</picker>
			</view>

			<view class="edit-info-box-end" @click="choiceSiteNameSave">
				<button type="primary">绑定</button>
			</view>

		
		</view>
		
		<uni-popup ref="ownershipStore" type="center" :maskClick="false" class="ownershipstore">
			<view class="box">
				<view class="head-box">
					<view class="h-box">
						<text>选择归属店铺</text>
						<i class="iconfont iconclose po-right" @click="closeOwnershipStore"></i>
					</view>
				</view>
				<scroll-view scroll-y="true"  style="height: 300px;" v-if="storeList.length > 0">
					<view class="body-box">
						<!-- <view class="store-item" :class="cur_active == index ? 'active' : '' "  @click="checkItem(index)" v-for="(item, index) in storeList.length" :key="index" :style=" cur_active == index ? 'backgroundImage: url(../../../static/belongshop/jubg.png)' : '' "> -->
						<view class="store-item" :class="cur_active == index ? 'active' : '' "  @click="checkItem(index,item)" v-for="(item, index) in storeList" :key="index" :style=" cur_active == index ? 'backgroundImage: url('+$util.img('upload/uniapp/belongshop/jubg.png') +')' : '' "> 
							<!-- :style=" 'backgroundImage: url('+$util.img('upload/uniapp/belongshop/bg.png') +')' "> -->
							<!-- <view class="bg-item" :style=" 'backgroundImage: url(../../../static/belongshop/bg.png)' "> -->
							<view>
								<!-- <image style="border-radius: 20rpx;width: 134rpx;height: 134rpx;margin-right: 20rpx;" src="../../../static/download/bgi.png" mode="aspectFit"></image> -->
								<image style="border-radius: 10rpx;width: 150rpx;height: 150rpx;margin-right: 20rpx;" :src="item.logo ? $util.img(item.logo) : $util.getDefaultImage().default_goods_img" mode="aspectFit"></image>
							</view>
							<view>
								<view class="store-name">{{item.site_name}}({{item.member_name}})</view>
								<text class="store-addr">{{item.full_address}}</text>
								<view class="mark" :class="cur_active == index ? 'active' : '' ">
									<text class="title">主营</text>
									<text class="tag">{{item.category_name}}</text>
								</view>
							</view>
							<view class="checklogo">
								<!-- <image style="width: 30rpx;height: 30rpx;" src="../../../static/belongshop/checked.png" mode="aspectFit"></image> -->
								<image style="width: 30rpx;height: 30rpx;" :src="$util.img('upload/uniapp/belongshop/checked.png')" mode="aspectFit"></image>
							</view>
						</view>
					</view>
				</scroll-view>
			
				<view class="empty-box" v-else>
					<view class="empty-content">
						<!-- <image style="width: 400rpx;height: 300rpx;" src="../../../static/belongshop/empty.png" mode="aspectFit"></image> -->
						<image style="width: 400rpx;height: 300rpx;" :src="$util.img('upload/uniapp/belongshop/empty.png')" mode="aspectFit"></image>
						<text class="record-empty no-record">暂无归属店铺</text>
					</view>
				</view>
				
				
				<view class="ensureStore" @click="guishu">
					<view class="txt">
						确定归属店铺
					</view>
				</view>
			</view>
		</uni-popup>
		
		
		
		<uni-popup ref="record" type="bottom" :maskClick="false" class="record">
			<view class="box">
				<view class="head-box">
					<view class="h-box">
						<text>店铺归属记录</text>
						<i class="iconfont iconclose po-right" @click="closeRecord"></i>
					</view>
				</view>
				<scroll-view scroll-y="true" v-if="RecordList.length > 0" style="height: 1040rpx;">
					<view class="body-box">
						<view class="record-item" v-for="item in RecordList" :key="item.id">
							<!-- <view class="bg-item" :style=" 'backgroundImage: url(../../../static/belongshop/bg.png)' "> -->
							<view class="bg-item" :style=" 'backgroundImage: url('+$util.img('upload/uniapp/belongshop/bg.png') +')' ">
								<view class="shop-name">
									{{ item.site_name }}
								</view>
								<view class="shop-time">
									归属时间：{{ $util.timeStampTurnTime(item.create_time) }}
								</view>
							</view>
							<view class="bg-text">
								{{ item.category_name }}
							</view>
						</view>
					</view>
				</scroll-view>
			
				<view class="empty-box" v-else>
					<view class="empty-content">
						<!-- <image style="width: 400rpx;height: 300rpx;" src="../../../static/belongshop/empty.png" mode="aspectFit"></image> -->
						<image style="width: 400rpx;height: 300rpx;" :src="$util.img('upload/uniapp/belongshop/empty.png')" mode="aspectFit"></image>
						<text class="record-empty no-record">暂无归属记录</text>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	export default {
		components:{
			uniPopup,
		},
		data() {
			return {
				ascriptionInfo: {
					code: '',
					site_name: ''
				},
				old_beblong_info: {
					code: '',
					site_name: ''
				},
				new_belong_info: {
					code: '',
					site_name: ''
				},
				shopList: [],

				index: 0,
				lng: '116.23',
				lat: '39.54',
				RecordList: [],
				cur_active: 0,
				storeList: [],
				param_code: null,
			}
		},
		computed: {
			//vueX页面主题
			themeStyle() {
				return 'theme-' + this.$store.state.index.themeStyle
			},
			accountType() {
				let arr = Object.keys(this.ascriptionInfo);
				let index = 0;
				if (!arr.length) {
					return index
				} else {
					for (let i = 0; i < arr.length; i++) {
						if (arr[i] == this.ascriptionInfo.withdraw_type) {
							index = i;
						}
					}
					return index
				}
			}
		},
		//uniapp原生导航栏右边自定义按钮或文字进行跳转
		onNavigationBarButtonTap(e) {
			this.$refs.record.open();
		},
		onShow() {
			// 刷新多语言
			// this.$langConfig.refresh();
			this.getBelongShopInfo();
			this.getShopList();
			this.getRecord();
		},
		methods: {
			guishu() {
				let that = this;
					uni.showModal({
						title: '提示',
						content: `确定要归属【${that.param_code.site_name}】吗？`,
						success: function(res) {
							if (res.confirm) {
								that.$api.sendRequest({
									url: '/api/member/belongShop',
									data: {
										code: that.param_code.code,
									},
									success: res => {
										that.$util.showToast({
											title: res.message,
										});
										if(res.code >= 0){
											that.closeOwnershipStore();
											setTimeout(function(){
												that.$util.redirectTo('/pages/member/index/index', {}, 'reLaunch');
											}, 1000);
										}
									},
					
								});
							} else if (res.cancel) {}
						}
					});
				
				
			},
			checkItem(index,item) {
				this.cur_active = index;
				this.param_code = item;
			},
			openOwnershipStore() {
				this.$refs.ownershipStore.open();
			},
			closeOwnershipStore() {
				this.$refs.ownershipStore.close();
			},
			getRecord() {
				this.$api.sendRequest({
					url: '/api/member/getBelongShopRecord',
					success: res => {
						if(res.code >= 0 && res.data) {
							this.RecordList = res.data.list;
						}else {
							this.$util.showToast({
								title: res.message
							})
						}
					},
					fail: err => {
						this.$util.showToast({
							title: err.message
						})
					}
				});
			},
			//关闭店铺归属弹框
			closeRecord() {
				this.$refs.record.close();
			},
			// 获取
			getBelongShopInfo() {
				this.$api.sendRequest({
					url: '/api/member/getBelongShopInfo',
					success: res => {
						console.log(res);
						this.old_beblong_info.code = res.data.code;
						this.old_beblong_info.site_name = res.data.site_name;
					}
				});
			},
			/**
			 * 获取店铺街方式
			 */
			getShopList() {
				// 经纬度
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						this.lng = res.longitude;
						this.lat = res.latitude;
						this.$api.sendRequest({
							url: '/api/shop/page',
							data: {
								page: 1,
								page_size: 20,
								order: 'distance',
								sort: 'asc',
								belong_right: 1,
								// 经纬度
								lng: this.lng,
								lat: this.lat
							},
							success: res => {
								if (res.code >= 0 && res.data) {
									let list = res.data.list;
									this.shopList = res.data.list;
								}
							}
						});
					},
					fail:(res) => {
						console.log('获取经纬度失败', res);
					},
				});
				
				
			},
			/**
			 * 选择店铺列表
			 */
			bindPickerChange: function(e) {
				this.index = e.detail.value
				this.new_belong_info.site_name = this.shopList[e.detail.value].site_name
				this.new_belong_info.code = this.shopList[e.detail.value].code
			},
			// 绑定
			choiceSiteNameSave() {
				
				
				
				
				let that = this;
				if (that.new_belong_info.code == '') {
					that.$util.showToast({
						title: '请输入商铺编号',
					});
					return
				}
				
				
				//弹框选择归属店铺
				//获取店铺列表
				this.$api.sendRequest({
					url: '/api/member/getBelongShopInfoByCode',
					data: {
						code: that.new_belong_info.code
					},
					success: res => {
						if(res.code >= 0 && res.data) {
							this.storeList = res.data;
							this.param_code = this.storeList[0];
						}else {
							this.$util.showToast({
								title: res.message,
							})
						}
					},
					fail: err => {
						this.$util.showToast({
							title: err.message,
						})
					}
				});
				
				this.openOwnershipStore();
				
								
			},
		},

	}
</script>

<style lang="scss" scoped>
	.info-list-cell {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 40rpx;
		position: relative;
		line-height: 50rpx;
		background-color: #fff;

		&.log-out-btn {
			margin-top: 40rpx;

			.cell-tit {
				margin: auto;
			}
		}


		&.info-item {
			margin-top: 16rpx;
		}

		&.info-list-con:after {
			content: '';
			position: absolute;
			left: 30rpx;
			right: 30rpx;
			bottom: 0;
			border-bottom: 1px solid $ns-bg-color-gray;
		}

	}

	.edit-info-box {
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 40rpx;
		min-height: 50rpx;
		background-color: #fff;

		.info-name {
			width: 180rpx;
			font-size: $ns-font-size-base;
			text-align: left;
		}

		.info-content {
			&:first-of-type {
				width: auto !important;
			}

			margin-right: auto;
			width: 300rpx;
			font-size: $ns-font-size-base;
			padding: 0;

		}

		.info-content-frist {
			margin-left: -17px !important;
		}

		.dynacode {
			margin: 0;
			padding: 0 10rpx;
			width: 250rpx;
			height: 60rpx;
			font-size: $ns-font-size-base;
			line-height: 60rpx;
			color: #fff;
			word-break: break-all;
		}
		

	}

	.pick_wid {
		width: 200rpx;
	}
	uni-button {
	    margin: 0 30px;
	    font-size: 14px;
	    border-radius: 20px;
	    line-height: 2.7;
	    margin-top: 15px;
	}
	
	.ownershipstore .box {
		width: 600rpx;
		
		.head-box {
			top:-158rpx;
			left:0;
			width: 100%;
			height: 120rpx;
			box-sizing: border-box;
			padding: 40rpx 40rpx 60rpx 40rpx;
			.h-box {
				position: relative;
				text-align: center;
				// color: #303133;
				// line-height: 56rpx;
				// height: 56rpx;
				// font-size: 30rpx;
				
				
				//width: 202px;
				height: 32rpx;
				font-size: 34rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #303133;

				
				.po-right {
					position: absolute;
					right: 0;
				}
			}
			
		
		}
		.body-box {
			width: 100%;
			height: 1080rpx;
			box-sizing: border-box;
			padding: 0 30rpx;
			
			
			.active {
				border: 2rpx solid #FF4544 !important;
				background-color: #fff !important;
			}
			.store-item {
				display: flex;
				box-sizing: border-box;
				padding: 20rpx 20rpx;
				background-color: #F8F8F8;
				margin-bottom: 20rpx;
				border-radius: 10rpx;
				border: 2rpx solid transparent;
				background-repeat: no-repeat;
				background-size: 10% 20%;
				background-position: right bottom;
				position: relative;
				
				.checklogo {
					position: absolute;
					width: 30rpx;
					height: 30rpx;
					right: 6rpx;
					bottom: 4rpx;
				}
								
				.store-name {
					width: 350rpx;
					height: 50rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #303133;
					white-space:nowrap;
					text-overflow:ellipsis;
					overflow:hidden;
				}
				
				.store-addr {
					width: 350rpx;
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #909399;
					display: inline-block;
					white-space:nowrap;
					text-overflow:ellipsis;
					overflow:hidden;
					line-height: 1.2;
				}
				
				.mark {
					display: inline-block;
					border: 2rpx solid #CCCCCC;
					line-height: 1.2;
					font-size: 24rpx;
					.title {
						background-color: #CCCCCC;
						color:#fff;
						padding: 4rpx 4rpx;
					}
					.tag {
						color: #909399;
						padding: 4rpx 4rpx;
					}
				}
				.active {
					border: 2rpx solid #FF4544;
					
					.title {
						background-color: #FF4544;
						color:#fff;
						padding: 4rpx 4rpx;
					}
					.tag {
						color: #FF4544;
						padding: 4rpx 4rpx;
					}
				}
			}
			
			
		}
		
		
		.empty-box {
			.empty-content {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				padding: 30rpx 30rpx;
				
				.record-empty {
					
					margin-top: 20rpx;
				}
				.zanwu {
					width: 117px;
					height: 28px;
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #303133;
					text-align: center;
				}
				.no-record {
					width: 194px;
					height: 26px;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #909399;
					text-align: center;
				}
			}
		}
		.ensureStore {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100%;
			margin: 30rpx auto;
			
			.txt {
				background-color: pink;
				width: 90%;
				text-align: center;
				height: 88rpx;
				line-height: 88rpx;
				background: linear-gradient(90deg, #FF4544 0%, #FF6344 100%);
				border-radius: 44rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;

			}
			
		}
	}
	
	
	.record .box {
		
		.head-box {
			top:-158rpx;
			left:0;
			width: 100%;
			height: 158rpx;
			box-sizing: border-box;
			padding: 40rpx 40rpx 60rpx 40rpx;
			.h-box {
				position: relative;
				text-align: center;
				color: #303133;
				line-height: 56rpx;
				height: 56rpx;
				font-size: 30rpx;
				
				.po-right {
					position: absolute;
					right: 0;
				}
			}
			
		
		}
		.body-box {
			width: 100%;
			height: 1080rpx;
			box-sizing: border-box;
			padding: 0 30rpx;
			
			.record-item {
				position: relative;
				background: linear-gradient(263deg, #FFF5F4 0%, #FFF9F8 100%);
				width: 100%;
				margin-bottom: 30rpx;
				border-radius: 16rpx;
				
				.bg-text {
					position: absolute;
					right:24rpx;
					top:4rpx;
					color:#fff;
					width: 120rpx;
					text-align: center;
				}
				
				.bg-item {
					box-sizing: border-box;
					padding:20rpx 20rpx;
					background-repeat: no-repeat;
					background-size: 30% 40%;
					background-position: right top;
					
					.shop-name{
						width: 168px;
						height: 32px;
						font-size: 34rpx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #303133;
					
					}
					
					.shop-time {
						width: 393px;
						height: 26px;
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #909399;
					}
				}
				
				
				
				
			}
		}
		
		
		.empty-box {
			.empty-content {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				padding: 30rpx 30rpx;
				
				.record-empty {
					
					margin-top: 20rpx;
				}
				.zanwu {
					width: 117px;
					height: 28px;
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #303133;
					text-align: center;
				}
				.no-record {
					width: 194px;
					height: 26px;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #909399;
					text-align: center;
				}
			}
		}
		
	}
	
	
	
	
</style>

<style lang="scss" scoped>
	/deep/.record .uni-popup__wrapper.bottom{
		border-radius: 40rpx 40rpx 0 0 ;
		height: 93%;
	}
	/deep/.record .uni-popup__wrapper-box {
		border-radius: 40rpx 40rpx 0 0;
	}
	
	/deep/.record .uni-popup__wrapper.uni-custom.bottom .uni-popup__wrapper-box,
	 .uni-popup__wrapper.uni-custom.top .uni-popup__wrapper-box {
		 max-height: 900px;
	 }
	 
	/deep/ .ownershipstore .uni-popup__wrapper-box {
		border-radius: 20rpx 20rpx;
	}
</style>
